package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Visitor;
import com.itheima.service.mongo.VisitorService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class VisitorServiceImpl implements VisitorService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public List<Visitor> findVisitorHead4(Long userId, Long userLoginTime) {
        Query query = new Query(
                Criteria.where("userId").is(userId) //查看谁的访客
                .and("date").gt(userLoginTime)//查询大于上一次查看访客时间即可
        ).with(Sort.by(Sort.Order.desc("date")))  //时间降序
                .skip(0).limit(4);//只查询4条记录

        List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
        return visitorList;
    }
}
